<?php

class Ead_MinhacontaController extends App_Controller_Action
{
    private $_PER_ID;
    private $_USU_ID;
    
    public function init()
    {
        /* Initialize action controller here */
        $auth = Zend_Auth::getInstance();
        $s_user = $auth->getStorage();
        $this->_PER_ID = $s_user->read()->PER_ID;
        $this->_USU_ID = $s_user->read()->USU_ID;
    }

    public function indexAction() {
        Zend_Loader::loadClass('tools','library');
        Zend_Loader::loadClass('datetools','library');
        Zend_Loader::loadClass('compras','library');
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/jquery.maskMoney.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/jquery.maskedinput.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/mask.js');
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/script_sis.js');
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/s_minhaconta.js');
        
        $this->view->status = compras::returnStatus();
        
        $id = $this->_USU_ID;
        
        $form = new Ead_Form_Minhaconta();

        $this->view->form = $form;
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {

                $data = array(
                    'USU_ID'        => $id,	
                    'USU_CEP'       => $form->getValue('USU_CEP'),
                    'USU_ENDERECO'  => $form->getValue('USU_ENDERECO'),
                    'USU_BAIRRO'    => $form->getValue('USU_BAIRRO'),
                    'USU_CIDADE'    => $form->getValue('USU_CIDADE'),
                    'USU_UF'        => $_REQUEST['uf'],
                    'USU_TEL'       => $form->getValue('USU_TEL'),
                    'USU_PER1'      => $form->getValue('USU_PER1'),
                    'USU_PER2'      => $form->getValue('USU_PER2'),
                    'USU_PER3'      => $form->getValue('USU_PER3'),                    
                );
                
                $auth = Zend_Auth::getInstance();
                $s_user = $auth->getStorage();
                $s_user->read()->USU_CADSTA = 1; 
                //$auth->getStorage()->write($s_user);
                
                if(strlen($form->getValue('USU_SENHA')) > 1){
                    $data['USU_SENHA'] = sha1($form->getValue('USU_SENHA'));
                }
                $obj = new Ead_Model_DbTable_Usuarios();

                if ($id == 0)
                    $obj->insert($data);
                else
                    $obj->update($data, 'USU_ID = ' . $id);

                $this->_helper->redirector('index');

            } else {
                $id = $this->_USU_ID;
            
                if ($id > 0) {
                    $obj = new Ead_Model_DbTable_Usuarios();
                    $row = $obj->fetchRow('USU_ID = ' . $id)->toArray();
                    $row['USU_DTNASC'] = Datetools::amd_dma($row['USU_DTNASC']);
                    $form->populate($row);
                }

                $obj = new Ead_Model_DbTable_Compra();
                $this->view->cursos = $obj->fetchAll('USU_ID = '. $this->_USU_ID.' and COM_STATRA = "Completo"');


                $obj = new Ead_Model_DbTable_Compra();
                $this->view->compra = $obj->fetchAll($obj->select()->where('USU_ID = '. $this->_USU_ID)->order('COM_DATHOR DESC'));
            
                echo "<script> $(function(){ $('#form_minhaconta').fadeIn(); }); </script>";
            }
        } else {
            $id = $this->_USU_ID;
            
            if ($id > 0) {
                $obj = new Ead_Model_DbTable_Usuarios();
                $row = $obj->fetchRow('USU_ID = ' . $id)->toArray();
                $row['USU_DTNASC'] = Datetools::amd_dma($row['USU_DTNASC']);
                $form->populate($row);
            }
            
            $obj = new Ead_Model_DbTable_Compra();
            $this->view->cursos = $obj->fetchAll('USU_ID = '. $this->_USU_ID.' and COM_STATRA = "Completo"');
            
            
            $obj = new Ead_Model_DbTable_Compra();
            $this->view->compra = $obj->fetchAll($obj->select()->where('USU_ID = '. $this->_USU_ID)->order('COM_DATHOR DESC'));
            
        }
        
    }
    
    public function concluircadastroAction() {
        Zend_Loader::loadClass('tools','library');
        Zend_Loader::loadClass('datetools','library');
        Zend_Loader::loadClass('compras','library');
       
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/jquery.maskMoney.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/jquery.maskedinput.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/mask.js');
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/script_sis.js');
        
        $this->view->status = compras::returnStatus();
        
        $id = $this->_USU_ID;
        
        $form = new Ead_Form_Concluircadastro();
        $form->submit->setLabel('Gravar');

        $this->view->form = $form;
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {

                $data = array(
                    'USU_ID'        => $id,	
                    'USU_CADSTA'    => 1,
                    'USU_CPF'       => $form->getValue('USU_CPF'),
                    'USU_DTNASC'    => Datetools::dma_amd($form->getValue('USU_DTNASC')),
                    'USU_CEP'       => $form->getValue('USU_CEP'),
                    'USU_ENDERECO'  => $form->getValue('USU_ENDERECO'),
                    'USU_BAIRRO'    => $form->getValue('USU_BAIRRO'),
                    'USU_CIDADE'    => $form->getValue('USU_CIDADE'),
                    'USU_TEL'       => $form->getValue('USU_TEL'),
                    'USU_PER1'      => $form->getValue('USU_PER1'),
                    'USU_PER2'      => $form->getValue('USU_PER2'),
                    'USU_PER3'      => $form->getValue('USU_PER3'),                    
                );
                
                $auth = Zend_Auth::getInstance();
                $s_user = $auth->getStorage();
                $s_user->read()->USU_CADSTA = 1; 
                //$auth->getStorage()->write($s_user);
                
                if(strlen($form->getValue('USU_SENHA')) > 1){
                    $data['USU_SENHA'] = sha1($form->getValue('USU_SENHA'));
                }
                $obj = new Ead_Model_DbTable_Usuarios();

                if ($id == 0)
                    $obj->insert($data);
                else
                    $obj->update($data, 'USU_ID = ' . $id);

                return $this->_helper->redirector('index', 'index', array());
            } else {
                $form->populate($formData);
            }
        } else {
            $id = $this->_USU_ID;
            
            if ($id > 0) {
                $obj = new Ead_Model_DbTable_Usuarios();
                $row = $obj->fetchRow('USU_ID = ' . $id)->toArray();
                $row['USU_DTNASC'] = Datetools::amd_dma($row['USU_DTNASC']);
                $form->populate($row);
            }
        }
        
    }
    
}